<template>
  <div class="home">
    <el-container style="height: 100%;border: 1px solid #eee">
      <el-header>
        <h2 style="margin: 0px 0px;width: 200px;">
          宿舍管理系统
        </h2>
        <div style="flex: 1;">
          <span>首页 / </span>
          <span>{{ $store.state.userInfo.account }} </span>
          <span style="color: red;">({{ $store.state.userInfo.msg }})</span>
        </div>
        <el-dropdown @command="handleCommand">
          <span class="el-dropdown-link">
            <el-avatar size="large" :src="require('../assets/logo.png')"></el-avatar><i
              class="el-icon-arrow-down el-icon--right"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="userInfo">个人信息</el-dropdown-item>
            <el-dropdown-item command="logout">注销</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-header>
      <el-container>
        <el-aside width="200px">
          <el-menu @select="handleMenuSelected" :router="true">
            <el-menu-item v-for="item in menus" :class="{'is-active' : item.href == $store.state.menu_active}"
                          :key="item.id" :index="item.href">
              <i class="el-icon-setting"></i>
              <span slot="title">{{ item.title }}</span>
            </el-menu-item>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view/>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
// @ is an alias to /src
// import HelloWorld from '@/components/HelloWorld.vue';
import menusApi from "@/http/menusApi";
import loginApi from "@/http/loginApi";

export default {
  name: 'Home',
  data() {
    return {
      menus: [
        {id: 1, title: "首页", href: "/welcome.html", roles: "0,1,2"},
        {id: 2, title: "宿舍管理员管理", href: "/dormAdmins.html", roles: "0,1"},
        {id: 3, title: "学生管理", href: "/students.html", roles: "0,1"},
      ]
    }
  },
  methods: {
    handleCommand(command) {
      console.log('click on item ' + command);
      if (command == "logout") {
        loginApi.logout().then(res => {
          if (res.data.code == 200) {
            this.$message.info(res.data.msg);
            sessionStorage.clear();
            this.$router.replace("/login");
          }
        })
      }
    },
    handleMenuSelected(index, indexPath) {
      console.log(index, indexPath);
      this.$store.commit("SET_MENU_ACTIVE", index);
    }
  },
  created() {
    this.$store.dispatch('GET_DORMS_ACTIONS');
    this.$store.dispatch('GET_USERINFO_ACTIONS');
    menusApi.findAll().then(res => {
      if (res.data.code == 200) {
        this.menus = res.data.data;
      }
    });
  }
};
</script>

<style>
.home {
  width: 100%;
  height: 100%;
  /*position: fixed;*/
}

.el-header {
  background-color: #B3C0D1;
  color: #333;
  display: flex;
  align-items: center;
}

.el-aside {
  background-color: #D3DCE6;
  color: #333;
  line-height: 200px;
}

.el-main {
  background-color: #E9EEF3;
  color: #333;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
</style>
